package com.example.micro.jvm.server.classloader.custom;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class BoluoClassLoader extends ClassLoader {

    private final static String fileSuffixExt = ".class";

    private String classLoaderName;

    private String loadPath;

    public void setLoadPath(String loadPath) {
        this.loadPath = loadPath;
    }

    /**
     * 指定当前父类加载器
     *
     * @param parent
     * @param classLoaderName
     */
    public BoluoClassLoader(ClassLoader parent, String classLoaderName) {
        super(parent);
        this.classLoaderName = classLoaderName;
    }

    /**
     * 使用AppClassLoader 作为本类加载器
     *
     * @param classLoaderName
     */
    public BoluoClassLoader(String classLoaderName) {
        super();
        this.classLoaderName = classLoaderName;
    }

    /**
     * @param parent
     */
    public BoluoClassLoader(ClassLoader parent) {
        super(parent);
    }

    private byte[] loadClassData(String name) {
        byte[] data = null;
        ByteArrayOutputStream baos = null;
        InputStream is = null;

        try {
            name = name.replace(".", "\\");
            String fileName = loadPath + name + fileSuffixExt;
            File file = new File(fileName);
            is = new FileInputStream(file);

            baos = new ByteArrayOutputStream();
            int ch;
            while ((ch = is.read()) < 0) {
                baos.write(ch);
            }
            data = baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != baos) {
                    baos.close();
                }
                if (null != is) {
                    is.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return data;
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        byte[] data = loadClassData(name);
        System.out.println("BoluoClassLoader 加载我们的类:===>" + name);
        return defineClass(name, data, 0, data.length);
    }

    //正常情况下,把我们的AppIsCustParentDemo放到D:\\smlz的目录下
    public static void main(String[] args) throws ClassNotFoundException {

//        BoluoClassLoader boluoClassLoader = new BoluoClassLoader("tulingClassLoader");
//        //设置加载路径
//        boluoClassLoader.setLoadPath("\\Users\\didi\\workspace\\alibaba-cloud-demo\\micro-jvm-server\\");
//        //通过自定义类加载器加载我们的AppIsCustParentDemo
//        Class<?> targetClass = boluoClassLoader.loadClass("com.example.micro.jvm.server.classloader.custom.Person");
//        System.out.println("targetClass 被class加载器加载..." + targetClass.getClassLoader());

        System.out.println(System.getProperty("java.class.path"));
        //调用加载当前类的类加载器（这里即为系统类加载器）加载TestBean
        Class typeLoaded = Class.forName("com.example.micro.jvm.server.classloader.Person");
        //查看被加载的TestBean类型是被那个类加载器加载的
        System.out.println(typeLoaded.getClassLoader());

    }


}


